﻿using System;
using System.Collections.Generic;
using System.Linq;
using Ease.EaseNode.Runtime;
using Ease.EaseNode.Runtime.Param;

namespace Ease.TestEaseNode.Runtime
{
    public partial class TestDataManager
    {
#if UNITY_EDITOR

        public TestDataNodeInspector testDataNodeInspector
        {
            get => GetComponent<TestDataNodeInspector>();
        }

        public TestSetting Setting
        {
            get => GetComponent<TestSetting>();
        }

        private CommonNodeDataManager<BaseTestData> helper;

        public INodeDataManager NodeDataManager
        {
            get
            {
                if (helper == null)
                {
                    helper = new CommonNodeDataManager<BaseTestData>(datas, new List<Type> { typeof(TestData), typeof(TestData2) });
                    helper.OnDataCreateEvent += (a) => { a.id = datas.Count; };
                }

                return helper;
            }
        }

        private List<IEaseNodeDataState> States => NodeDataManager.States;


        public event Action<BaseChangeParam> OnValueChangeEvent;

        public void OnValueChange(BaseChangeParam param)
        {
            OnValueChangeEvent?.Invoke(param);
            foreach (var baseEaseNodeDataState in States)
            {
                baseEaseNodeDataState.OnValueChange(param);
            }
        }

        public void OnUpdateSelection(string key)
        {
            if (string.IsNullOrEmpty(key))
                testDataNodeInspector.state = null;
            else
                testDataNodeInspector.state = States.FirstOrDefault(x => x.Key.Equals(key)) as BaseEaseNodeDataState;
        }
#endif
    }
}